function patch = crop_patch2(im, pos, target_sz, pad)
    avg_dim = sum(target_sz)/2;
    sz = target_sz+round(pad*avg_dim);
    xs = floor(pos(2)) + (1:sz(2)) - floor(sz(2)/2);
    ys = floor(pos(1)) + (1:sz(1)) - floor(sz(1)/2);
    
    %check for out-of-bounds coordinates, and set them to the values at
    %the borders
    xs(xs < 1) = 1;
    ys(ys < 1) = 1;
    xs(xs > size(im,2)) = size(im,2);
    ys(ys > size(im,1)) = size(im,1);
    
    patch = im(ys, xs, :);
end

